﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using TextAnalytics.Models;
using TextAnalytics.Operations;

namespace TextAnalytics
{
    public class ProjectService
    {
        private ProjectService()
        {
            CheckedFiles = new List<string>();
            AvailableFiles = new List<string>();
        }
        public static readonly ProjectService Default = new ProjectService();

        public bool HasOpenedProject
        {
            get
            {
                return openedProject != null;
            }
        }
        public bool HasCheckedFiles
        {
            get
            {
                return CheckedFiles.Count > 0;
            }
        }
        public bool HasSelectedFile
        {
            get
            {
                return !string.IsNullOrWhiteSpace(selectedFile.Path);
            }
        }

        private ProjectModel openedProject;
        public ProjectModel OpenedProject
        {
            set
            {
                openedProject = value;
                DidOpenProject(this, new OpenProjectEventArgs() { OpenProject = value });
            }
        }
        private FileModel selectedFile;
        private FileModel SelectedFile
        {
            set
            {
                selectedFile = value;
                DidSelectFile(this, new SelectFileEventArgs() { SelectedFile = value });
            }
        }
        public string GetSelectedFileContent()
        {
            if (string.IsNullOrWhiteSpace(selectedFile.Path))
            {
                return null;
            }

            if (!File.Exists(selectedFile.Path))
            {
                return null;
            }
            string text = null;
            try
            {
                text = File.ReadAllText(selectedFile.Path);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while reading file: {0}", selectedFile.Path);
                Trace.WriteLine(ex.ToString());
            }
            return text;
        }
        public List<string> CheckedFiles { get; private set; }
        private List<string> AvailableFiles = new List<string>();

        public void CloseOpenedProject()
        {
            openedProject = null;
            selectedFile = null;
            CheckedFiles.Clear();
            AvailableFiles.Clear();
            DidCloseProject(this, EventArgs.Empty);
        }
        public void ChangeSelectedFile(FileModel file)
        {
            SelectedFile = file;
            var fileContent = GetSelectedFileContent();
            if (string.IsNullOrEmpty(fileContent)) return;

            try
            {
                var splitResult = TextSplitter.Default.Split(fileContent);
                DidSplitSelectedFiles(this, new TextSplitEventArgs(splitResult));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Trace.WriteLine(ex.ToString());
            }
        }

        public event Action<ProjectService, TextSplitEventArgs> DidSplitSelectedFiles = (sender, args) => { };
        public event Action<ProjectService, SelectFileEventArgs> DidSelectFile = (sender, args) => { };
        public event Action<ProjectService, OpenProjectEventArgs> DidOpenProject = (sender, eventArgs) => { };
        public event Action<ProjectService, EventArgs> DidCloseProject = (sender, args) => { };
    }

    public class TextSplitEventArgs : EventArgs
    {
        public TextSplitEventArgs()
        {

        }
        public TextSplitEventArgs(TextSplitResultModel textSplitResult)
        {
            TextSplitResult = textSplitResult;
        }
        public TextSplitResultModel TextSplitResult { get; set; }
    }
    public class OpenProjectEventArgs : EventArgs
    {
        public ProjectModel OpenProject { get; set; }
    }
    public class SelectFileEventArgs : EventArgs
    {
        public FileModel SelectedFile;
    }
}
